// Example: UIClone Node
import { app } from "../../../../scripts/app.js";
import { createCustomHTMLWidget } from "../../core/ui_core.js";

app.registerExtension({
  name: "FoW_Suite.UIClone",
  async beforeRegisterNodeDef(nodeType, nodeData, app) {
    if (nodeData.name === "UIClone") {
      nodeType.prototype.onNodeCreated = function () {
        this.inputs_offset = nodeData.name.includes("selective") ? 1 : 0;
        this.addWidget("button", "Update inputs", null, () => {
          if (!this.inputs) {
            this.inputs = [];
          }
          const target_number_of_inputs = this.widgets.find(w => w.name === "inputcount")["value"];
          if (target_number_of_inputs === this.inputs.length - this.inputs_offset) return; // already set, do nothing

          if (target_number_of_inputs < this.inputs.length - this.inputs_offset) {
            for (let i = this.inputs.length; i >= this.inputs_offset + target_number_of_inputs; i--) {
              this.removeInput(i);
            }
          } else {
            for (let i = this.inputs.length + 1 - this.inputs_offset; i <= target_number_of_inputs; ++i) {
              this.addInput(`conditioning_${i}`, "CONDITIONING");
            }
          }
          this.update();
        });
        createCustomHTMLWidget(this, 'test_message', "Hello from UIClone.js! (testing path)");
      };
    }
  },
  VERSION: "0.1.0", // Added version tracking
});